package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import butterknife.BindView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseSearchListFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.BaseParams;
import com.isunland.managesystem.entity.LocaleWorkCropInfoSub;
import com.isunland.managesystem.entity.SimpleDetailParams;
import com.isunland.managesystem.entity.rProductsListMain;
import com.isunland.managesystem.utils.MyStringUtil;
import com.isunland.managesystem.utils.MyUtils;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import com.isunland.managesystem.utils.ToastUtil;
import com.isunland.managesystem.widget.SingleLineView;
import java.util.UUID;

import static com.isunland.managesystem.common.ApiConst.URL_DELETE_LOCALE_WORK_CROP_INFO_SUB;
import static com.isunland.managesystem.common.ApiConst.URL_UPDATE_LOCALE_WORK_CROP_INFO_SUB;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/4/16 14:48
 */
public class LocaleWorkCropInfoSubDetailFragment extends SimpleDetailFragment<LocaleWorkCropInfoSub>
    implements View.OnClickListener {

  public static final int REQUEST_CROP_INFO = 4;
  private static final int MENU_ITEM_CROP_CHECK = 5;
  private static final int REQUEST_CROP_CHECK = 6;

  @BindView(R.id.sl_cropInfoTypeName) SingleLineView slCropInfoTypeName;
  @BindView(R.id.sl_cropInfoNo) SingleLineView slCropInfoNo;
  @BindView(R.id.sl_cropInfoNum) SingleLineView slCropInfoNum;
  @BindView(R.id.sl_technicalParam) SingleLineView slTechnicalParam;
  @BindView(R.id.sl_cropInfoMunit) SingleLineView slCropInfoMunit;
  private String mCropInfoTypeCode;

  @Override protected SimpleDetailParams initParams() {
    SimpleDetailParams params = new SimpleDetailParams();
    params.setLayoutId(R.layout.fragment_locale_work_crop_info_sub_detail);
    params.setTitleId(R.string.localeWorkCropInfoSub);
    return params;
  }

  @Override protected void initViewDetail(LocaleWorkCropInfoSub mItem) {
    bindView(mItem);
    slCropInfoMunit.setInputEnabled(false);
    slCropInfoNo.setInputEnabled(false);
    slCropInfoNum.setInputEnabled(false);
    slCropInfoTypeName.setInputEnabled(false);
    slTechnicalParam.setInputEnabled(false);
  }

  @Override protected void initViewEdit(LocaleWorkCropInfoSub mItem) {
    bindView(mItem);
    setOnClick();
  }

  @Override protected void initViewAdd() {
    setOnClick();
  }

  protected void bindView(LocaleWorkCropInfoSub mItem) {
    slCropInfoMunit.setTextContent(mItem.getCropInfoMunit());
    slCropInfoNo.setTextContent(mItem.getCropInfoNo());
    slCropInfoNum.setTextContent(MyStringUtil.valueOf(mItem.getCropInfoNum()));
    slCropInfoTypeName.setTextContent(mItem.getCropInfoTypeName());
    slTechnicalParam.setTextContent(mItem.getTechnicalParam());
    mCropInfoTypeCode = mItem.getCropInfoTypeCode();
  }

  protected void setOnClick() {
    slCropInfoTypeName.setOnClickContentListener(this);
  }

  @Override public void onClick(View v) {

    if (!(v.getTag() instanceof Integer)) return;
    int id = (int) v.getTag();
    if (id == R.id.sl_cropInfoTypeName) {
      selectCropInfo();
    }
  }

  private void selectCropInfo() {
    BaseVolleyActivity.newInstance(this, CropInfoSearchListActivity.class, null, REQUEST_CROP_INFO);
  }

  @Override protected void volleyPostInit() {

  }

  @Override protected void bindData(LocaleWorkCropInfoSub mItem) {

  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode != Activity.RESULT_OK) return;
    if (requestCode == REQUEST_CROP_INFO && data != null) {
      rProductsListMain value =
          (rProductsListMain) data.getSerializableExtra(BaseSearchListFragment.EXTRA_ITEM);
      slCropInfoTypeName.setTextContent(value.getProductName());
      slCropInfoNo.setTextContent(value.getMtype());
      mCropInfoTypeCode = value.getId();
      slCropInfoMunit.setTextContent(value.getMunit());
      slTechnicalParam.setTextContent(value.getMainFunctionDesc());
    }
    if (requestCode == REQUEST_CROP_CHECK) {
      mActivity.setResult(Activity.RESULT_OK);
      mActivity.finish();
    }
  }

  @Override protected void volleyPostConfirm() {

    if (slCropInfoTypeName.isEmptyContent()) {
      ToastUtil.show(R.string.completeWrite);
      return;
    }
    String url = ApiConst.getAbsoluteUrl(URL_UPDATE_LOCALE_WORK_CROP_INFO_SUB);
    ParamsNotEmpty params = new ParamsNotEmpty();
    params.put("mainId", mBaseParams.getId());
    params.put("id", mBaseParams.getType() == BaseParams.TYPE_ADD ? UUID.randomUUID().toString()
        : mItem.getId());
    params.put("cropInfoTypeCode", mCropInfoTypeCode);
    params.put("cropInfoTypeName", slCropInfoTypeName.getTextContent());
    params.put("cropInfoNo", slCropInfoNo.getTextContent());
    params.put("cropInfoNum", slCropInfoNum.getTextContent());
    params.put("technicalParam", slTechnicalParam.getTextContent());
    params.put("cropInfoMunit", slCropInfoMunit.getTextContent());

    MyUtils.showDialogLoading(mActivity);
    mActivity.volleyPost(url, params.getParams(), new SimpleDetailResponse());
  }

  @Override protected void volleyPostDelete() {
    String url = ApiConst.getAbsoluteUrl(URL_DELETE_LOCALE_WORK_CROP_INFO_SUB);
    ParamsNotEmpty params = new ParamsNotEmpty();
    if (mItem == null) return;
    params.put("id", mItem.getId());
    MyUtils.showDialogLoading(mActivity);
    mActivity.volleyPost(url, params.getParams(), new SimpleDetailResponse());
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    //农资详情时,增加menuj农资核查.
    if (mType == BaseParams.TYPE_DETAIL) {
      menu.add(0, MENU_ITEM_CROP_CHECK, 0, getString(R.string.cropCheck));
    }
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case MENU_ITEM_CROP_CHECK:
        BaseParams params = new BaseParams();
        params.setId(mItem.getId());
        OperationSubLogUpdateActivity.newInstance(this, OperationSubLogUpdateActivity.class, params,
            REQUEST_CROP_CHECK);
        return true;

      default:
        return super.onOptionsItemSelected(item);
    }
  }
}




